package com.example.rpapp.service;

import cn.hutool.core.collection.CollectionUtil;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.rpapp.price.command.PrizeAddCmdExe;
import com.example.rpapp.price.command.PrizeUpdateCmdExe;
import com.example.rpapp.price.query.PrizeListByParamQueryExe;
import com.example.rpclient.api.IPrizeService;
import com.example.rpclient.dto.PrizeAddCmd;
import com.example.rpclient.dto.PrizeUpdateCmd;
import com.example.rpclient.dto.data.PrizeVo;
import com.example.rpclient.dto.query.PrizeListByParamQuery;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;


@Slf4j
@Service
@AllArgsConstructor
public class PrizeServiceImpl implements IPrizeService {

    private final PrizeAddCmdExe prizeAddCmdExe;
    private final PrizeUpdateCmdExe prizeUpdateCmdExe;
    private final PrizeListByParamQueryExe prizeListByParamQueryExe;

    @Override
    public PrizeVo add(PrizeAddCmd cmd) {
        return prizeAddCmdExe.execute(cmd);
    }

    @Override
    public PrizeVo update(PrizeUpdateCmd cmd) {
        return prizeUpdateCmdExe.execute(cmd);
    }

    @Override
    public IPage<PrizeVo> page(PrizeListByParamQuery query) {
        return prizeListByParamQueryExe.execute(query);
    }

    @Override
    public PrizeVo one(Long id) {
        final var query = new PrizeListByParamQuery();
        query.setId(id);
        IPage<PrizeVo> page = page(query);
        if (CollectionUtil.isEmpty(page.getRecords())) {
            return null;
        }

        return page.getRecords().get(0);
    }
}
